<template>
  <div class="list-cards">
    <div class="list-top">
      <span class="title">{{ props.title }}</span>
      <span class="more-button" @click="handleMore"
        >更多<el-icon><ArrowRight /></el-icon
      ></span>
    </div>
    <ul class="list-content">
      <li
        class="list-item"
        v-for="(item, index) in listComputed"
        :key="index"
        @click="openPage(item)"
      >
        <div class="cover" :style="{ backgroundImage: `url(${item.src})` }"></div>
        <div class="item-info">
          <span class="name">{{ item.title }}</span>
        </div>
      </li>
      <li class="no-date"></li>
    </ul>
  </div>
</template>
<script setup name="ListCard">
import { computed, defineProps } from 'vue';
import { ArrowRight } from '@element-plus/icons-vue';
import { useRouter } from 'vue-router';
import isClientApp from '../../util/isClientApp';

const router = useRouter();

const props = defineProps({
  source: Array,
  typeName: String,
  title: String,
  recordLog: Function,
});

const listComputed = computed(() => {
  return props.source.map((item) => {
    return {
      ...item,
      src: props.typeName === '书签' ?  item.screenshot : item.src,
      title: props.typeName === '便签' ? item.name : item.title,
    };
  });
});

const openPage = ({ url, id }) => {
  props.recordLog(id);
  switch (props.typeName) {
    case '书签':
      isClientApp() ? window.myApi.sendOpenSellUrl(url) : window.open(url);
      break;
    case '便签':
      router.push({ path: '/index/memoEdit', query: { id } });
      break;
  }
};

const handleMore = () => {
  switch (props.typeName) {
    case '书签':
      router.push('/index/book');
      break;
    case '便签':
      router.push('/index/memo');
      break;
  }
};
</script>
<style scoped lang="less">
.list-cards {
  --card-bg-color: #ffffff;
  --card-radius: 18px;
  width: 100%;
  .list-top {
    display: flex;
    align-items: center;
    justify-content: space-between;
    font-weight: bold;
    margin-top: 26px;
    .title {
      transform: translateX(4px);
      font-size: 18px;
      color: #545454;
    }
    .more-button {
      color: var(--system-primary-color);
      transform: translateX(-4px);
      cursor: pointer;
      font-size: 16px;
      font-weight: normal;
      display: flex;
      align-items: center;
    }
  }
  .list-content {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    grid-template-rows: 160px;
    grid-column-gap: 14px;
    padding: 0;
    .list-item {
      width: 100%;
      height: 100%;
      border-radius: var(--card-radius);
      background-color: var(--card-bg-color);
      position: relative;
      transition: all 0.3s;
      cursor: pointer;
      overflow: hidden;
      .cover{
        width: 100%;
        height: 0;
        padding-bottom: 100%;
        overflow: hidden;
        background-position: center center;
        background-repeat: no-repeat;
        -webkit-background-size: cover;
        -moz-background-size: cover;
        background-size: cover;
      }
      &:hover {
        transform: translateY(-4px);
        transition: all 0.3s;
      }
      img {
        border-radius: var(--card-radius);
      }
      .item-info {
        position: absolute;
        bottom: 0;
        left: 0;
        z-index: 1;
        width: 100%;
        height: 36px;
        // background: #AAB3F3;
        background-color: rgba(0,0,0,0.18);
        border-bottom-left-radius: var(--card-radius);
        border-bottom-right-radius: var(--card-radius);
        backdrop-filter: blur(3px);
        overflow: hidden;
        box-sizing: border-box;
        padding: 8px 10px 0 10px;
        .name {
          color: #fff;
          font-size: 12px;
          font-weight: bold;
          white-space: nowrap;
          display: inline-block;
          width: 100%;
          text-overflow: ellipsis;
          overflow: hidden;
          // text-shadow: 0 0 2px #555;
        }
      }
    }
  }
}
</style>
